package com.android.webttest.View;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.android.webttest.R;
import com.android.webttest.View.FrameLayout.AIFragment;
import com.android.webttest.View.FrameLayout.GoodFragment;
import com.android.webttest.View.FrameLayout.MyFragment;
import com.android.webttest.utils.StatusBarUtil;
import com.android.webttest.utils.TokenManager;
import com.android.webttest.utils.Tools;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class UserManageLayout extends AppCompatActivity {


    private BottomNavigationView bottomNavigationView;
    private String authToken;

    private boolean doubleBackToExitPressedOnce = false;
    private Handler handler = new Handler(Looper.getMainLooper());
    private final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_user_manage_layout);
        // 初始化 TokenManager
        try {
            TokenManager.init(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 使用工具类设置状态栏
        StatusBarUtil.setImmersiveStatusBarR(this);
        //----------------------
        // 读取并解密 token
        try {
            String token = TokenManager.readAndDecryptToken(this);
            if (token != null) {
                // 处理 token，例如显示用户信息
                //todo
                authToken = token;
//                Tools.Toast(this, authToken);
            } else {
                // token 为空，可能需要重新登录
                Tools.Toast(this, "未找到有效的 token，需要重新登录");
                Intent intent = new Intent(UserManageLayout.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Tools.Toast(this, "读取 token 失败");
        }

        // 设置返回键处理
        OnBackPressedCallback callback = new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                if (doubleBackToExitPressedOnce) {
                    UserManageLayout.super.onBackPressed();
                    return;
                }
                doubleBackToExitPressedOnce = true;
                Toast.makeText(UserManageLayout.this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();

                handler.postDelayed(runnable, 2000); // 2秒内再次按下返回键才退出
            }
        };
        getOnBackPressedDispatcher().addCallback(this, callback);

        //
        bottomNavigationView = findViewById(R.id.user_bottom_menu);
        bottomNavigationView.setOnItemSelectedListener(item->{
            FragmentManager f=getSupportFragmentManager();
            FragmentTransaction transaction1= f.beginTransaction();
            int id=item.getItemId();//获取点击的item的ID
            if(id==R.id.user_bottom_menu_good){//点击了第一个item--商品
                transaction1.replace(R.id.user_home_frame,new GoodFragment());
                transaction1.commit();
            }else if(id==R.id.user_bottom_menu_ai){//点击了第二个item--AI
                transaction1.replace(R.id.user_home_frame,new AIFragment());
                transaction1.commit();
            }
            else if(id==R.id.user_bottom_menu_my){//点击了第三个item--我的
                transaction1.replace(R.id.user_home_frame,new MyFragment());
                transaction1.commit();
            }

            return true;
        });

        // 设置默认界面为 GoodFragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.user_home_frame, new GoodFragment());
        fragmentTransaction.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable); // 移除回调以避免内存泄漏
    }
}